package com.jyszkj.map.view

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.jyszkj.map.bean.DeviceBean
import com.jyszkj.osmdroid.map.R
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.Marker
import org.osmdroid.views.overlay.infowindow.MarkerInfoWindow

class CustomDeviceInfoWindow(
    mapView: MapView,
    terminalType: Int
) : MarkerInfoWindow(R.layout.layout_device_info_window,mapView) {

    private val textLayout: LinearLayout = mView.findViewById(R.id.layout_text)
    private val arrowIv: ImageView = mView.findViewById(R.id.iv_arrow)
    private val tvDeviceAddress: TextView = mView.findViewById(R.id.device_address)
    private val tvDeviceStatus: TextView = mView.findViewById(R.id.device_status)
    private val tvDeviceCode: TextView = mView.findViewById(R.id.device_code)
    private val list: MutableList<String> = mutableListOf()

    override fun onOpen(item: Any) {
        // 设置窗口内容
        val maker = item as Marker
        val obj = maker.relatedObject
        if(obj is DeviceBean) {
            textLayout.visibility = View.VISIBLE
            arrowIv.visibility = View.VISIBLE
            tvDeviceAddress.text = obj.address
            tvDeviceCode.text = obj.code
            when(obj.onlineStatus) {
                "0" -> {
                    //离线
                    tvDeviceStatus.text = "离线"
                    tvDeviceStatus.setTextColor(ContextCompat.getColor(mapView.context,R.color.offline_color))
                    textLayout.setBackgroundResource(R.drawable.bg_info_window_device_offline)
                }
                "1" -> {
                    //在线
                    tvDeviceStatus.text = "在线"
                    tvDeviceStatus.setTextColor(ContextCompat.getColor(mapView.context,R.color.online_color))
                    textLayout.setBackgroundResource(R.drawable.bg_info_window_device)
                }
                "2" -> {
                    //异常
                    tvDeviceStatus.text = "在线"
                    tvDeviceStatus.setTextColor(ContextCompat.getColor(mapView.context,R.color.online_color))
                    textLayout.setBackgroundResource(R.drawable.bg_info_window_device)
                }
            }
        } else {
            textLayout.visibility = View.GONE
            arrowIv.visibility = View.GONE
            return
        }


        // 添加动画效果
        // 添加动画效果
        mView.alpha = 0f
        mView.scaleX = 0.8f
        mView.scaleY = 0.8f
        mView.animate()
            .alpha(1f)
            .scaleX(1f)
            .scaleY(1f)
            .setDuration(400)
            .start()
    }
}